package cn.exrick.xboot.modules.transport.service.impl;

import cn.exrick.xboot.common.exception.XbootException;
import cn.exrick.xboot.common.utils.BeanConverterUtil;
import cn.exrick.xboot.modules.base.dao.UserDao;
import cn.exrick.xboot.modules.base.dao.mapper.UserRoleMapper;
import cn.exrick.xboot.modules.base.entity.UserRole;
import cn.exrick.xboot.modules.base.enums.RoleEnum;
import cn.exrick.xboot.modules.transport.mapper.BigCustomerUsersMapper;
import cn.exrick.xboot.modules.transport.entity.BigCustomerUsers;
import cn.exrick.xboot.modules.transport.mapper.BigCustomersMapper;
import cn.exrick.xboot.modules.transport.param.EditBigCustomerUsersParam;
import cn.exrick.xboot.modules.transport.param.QueryBigCustomerUsersParam;
import cn.exrick.xboot.modules.transport.service.BigCustomerUsersService;
import cn.exrick.xboot.modules.transport.vo.BigCustomerUsersVo;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.Proxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 大客户与用户关联信息接口实现
 * @author gj
 */
@Slf4j
@Service
@Transactional
public class BigCustomerUsersServiceImpl extends ServiceImpl<BigCustomerUsersMapper, BigCustomerUsers> implements BigCustomerUsersService {

    @Autowired
    private BigCustomerUsersMapper bigCustomerUsersMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private BigCustomersMapper bigCustomersMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCustomerUsers(EditBigCustomerUsersParam param) {
        BigCustomerUsers bigCustomerUsers = bigCustomerUsersMapper.selectOne(new LambdaQueryWrapper<BigCustomerUsers>().eq(BigCustomerUsers::getUserId, param.getCustomerId()).eq(BigCustomerUsers::getUserId, param.getUserId()));
        if(Objects.nonNull(bigCustomerUsers)){
            throw new XbootException("已绑定大客户"+ bigCustomersMapper.selectById(bigCustomerUsers.getCustomerId()).getCustomerName() + "不可重复绑定");
        }
        bigCustomerUsersMapper.insert(BeanConverterUtil.convertBean(param, BigCustomerUsers.class));
        if (Objects.isNull(userRoleMapper.selectOne(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId,param.getUserId()).eq(UserRole::getRoleName, RoleEnum.PLATFORM_UER.getValue())))){
            userRoleMapper.insert(new UserRole().setUserId(param.getUserId()).setRoleName(RoleEnum.PLATFORM_UER.getValue()));
        }
        return true;
    }

    @Override
    public IPage<BigCustomerUsersVo> getByCondition(Page page, QueryBigCustomerUsersParam param) {
        return bigCustomerUsersMapper.getByCondition(page, param);
    }

    @Override
    public BigCustomerUsersVo getBigCustomerUsersById(Long id) {
        return bigCustomerUsersMapper.getBigCustomerUsersById(id);
    }
}